package cn.hxzy.zyicon.service.impl;

import cn.hxzy.zyicon.entity.Resource;
import cn.hxzy.zyicon.entity.Role;
import cn.hxzy.zyicon.repository.ResourceRepository;
import cn.hxzy.zyicon.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2020-08-15 10:25:33
 */
@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceRepository resourceRepository;

    @Override
    public void delete(Integer id) {
        resourceRepository.deleteById(id);
    }

    @Override
    public void save(Resource resource) {
        resourceRepository.save(resource);
    }

    @Override
    public List<Resource> findAll() {
        List<Resource> all = resourceRepository.findByPid(0);
        for (Resource resource : all) {
         resource.setSon(resourceRepository.findByPid(resource.getId()));
        }
        return all;
    }

    @Override
    public Resource findById(Integer id) {
        Optional<Resource> byId =resourceRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    @Override
    public void update(Resource resource) {
        resourceRepository.save(resource);
    }
}